<template>
  <div class="login">
    <el-form
      v-loading="loading"
      class="form"
      :model="form"
      :rules="rules"
      ref="form"
    >
      <img class="logo" src="@/assets/common/login-logo.png" alt="" />
      <el-form-item prop="mobile">
        <el-input
          prefix-icon="el-icon-user"
          v-model="form.mobile"
          @keyup.enter.native="submit"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          prefix-icon="el-icon-unlock"
          v-model="form.password"
          @keyup.enter.native="submit"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" class="btn" @click="submit"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { validatePhone } from '@/utils/validate'
export default {
  data () {
    return {
      loading: false,
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'blur'
          },
          {
            validate: (rule, value, callback) => {
              validatePhone(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            }
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 12,
            message: '请输入6-12位密码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 登录
    submit () {
      this.$refs.form.validate(async result => {
        console.log(result)
        if (result) {
          // 调用vuex内的actions方法实现登录
          this.loading = true
          await this.$store.dispatch('user/getUserToken', this.form)
          this.loading = false
          this.$router.push('/')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  height: 100%;
  background: url(~@/assets/common/login.jpg) no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  .form {
    width: 500px;
    text-align: center;
    .logo {
      padding-bottom: 20px;
    }
    .btn {
      width: 100%;
      background: linear-gradient(to right, hotpink, skyblue);
      border-width: 0;
      color: #fff;
    }
  }
  ::v-deep .el-form-item__error {
    color: #fff;
  }
}
</style>
